const fs =  require('fs')
const path = require('path')
const {saveavatar,upavatarurl,getAvatarByUserId,getBgimgByUserId,upbgavatarurl,savebgimg,setfileinfo, getfilenames,getorfilename,deletefile} = require("../service/file")
class flie{
    async avatarInfo(ctx,next){
       // 1.用户的头像是哪一个文件呢?
    const { userId } = ctx.params;
    try {
    const {filename,mimetype } = await getAvatarByUserId(userId);
    ctx.response.set('content-type', mimetype);
    ctx.body = fs.createReadStream(`./uploads/avatar/${filename}`);
    } catch (error) {
      console.log(error);
    }
    }
    async avatar (ctx,next) {
        const {userid} = ctx.header
        const {filename, mimetype, size} = ctx.req.file
        console.log(filename, mimetype, size );
        try {
        ctx.body = await saveavatar(filename, mimetype, size,userid)
        } catch (error) {
          console.log(error);
        }
        //跟新用户表里的照片
        let url = `http://localhost:8081/users/${userid}/avatar` 
        await upavatarurl(url,userid)
      }
      async bgavatar(ctx,next){
        //设置背景照片文件
        console.log('来到了这');
        const {userid} = ctx.header
        const {filename, mimetype, size} = ctx.req.file
        console.log(filename, mimetype, size );
        try {
        ctx.body = await savebgimg(filename, mimetype, size,userid)
        } catch (error) {
          console.log(error);
        }
        //跟新用户表里的照片
        let url = `http://localhost:8081/users/${userid}/bgavatar` 
        await upbgavatarurl(url,userid)
      }
      //背景照片 
      async bgimginfo(ctx,next){
          // 1.用户的背景是哪一个文件呢?
     const { userId } = ctx.params;
    try {
    const {filename,mimetype } = await getBgimgByUserId(userId);
      ctx.response.set('content-type', mimetype);
      ctx.body = fs.createReadStream(`./uploads/avatar/${filename}`);
    } catch (error) {
      console.log(error);
    }
      }
      //处理大文件 
    async handlefile(ctx,next) {
      console.log('来到啦则');
      console.log(ctx.req.file);
      const {filename, mimetype, size,originalname} = ctx.req.file
      const {userid} = ctx.req.body
      console.log(filename, mimetype, size,userid,originalname);
      try {
      const res = await setfileinfo(filename, mimetype, userid,originalname)
      ctx.body = res[0]
      } catch (error) {
        console.log(error);
      }
    }
    //获取文件名称

    async getfilename (ctx,next) {
      const {userid}  = ctx.params
      console.log(userid);
      ctx.body =  await getfilenames(userid)
    }
    //文件下载 
    async downloadfile (ctx,next) {
      const {filename,userid} = ctx.params
      console.log(filename,userid);
      const {fileorname,mimetype} = await getorfilename(filename,userid)
      console.log(fileorname,mimetype);
      //拿到保存到服务器的文件名称 开始下载 
      try {
        ctx.response.set('content-type', mimetype);
        ctx.body = fs.createReadStream(`./file/bgfile/${fileorname}`);
      } catch (error) {
          console.log(error);
      }
    }
    async deletefile (ctx,next) {
      const  {name,id}  = ctx.request.body
      const res = await deletefile(name,id)
      try { 
      const {fileorname} =  await getorfilename(name,id)
      console.log(fileorname);
        if (fileorname) {
          fs.unlink(`./file/bgfile/${fileorname}`, (err)=>{
                console.log(err);
              })
              ctx.body ='文件删除成功'
        }
      } catch (error) {
        console.log(error);
      }
    }
}

module.exports = new flie()